import com.alibaba.fastjson.JSONObject;
import com.znyj.servlet.aes.AesException;
import com.znyj.servlet.aes.MsgCrypt;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * Created by gaoliwei02 on 2017/9/4.
 */
public class test_openDevice {
    public static void main(String[] args) throws InterruptedException, AesException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceId", "112173300000");
        jsonObject.put("port", 0);
        jsonObject.put("userId", "testuser");
        jsonObject.put("userLocation", "39.995665,116.480312");
        jsonObject.put("createTime", 1503645036000L);
        jsonObject.put("durationSec", 3600);
        jsonObject.put("actionMsg", 0);
        jsonObject.put("controlMsg", "");

        MsgCrypt msgCrypt = new MsgCrypt("tokenNoUse", "5lanyIYz49uJ3iqaKp8Zvd2j9xJ7rxZKRHBjcQeXBoq");
        String body = msgCrypt.encryptMsg(jsonObject.toString());

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://localhost:9999/outer_api/openDevice");

        String result = null;
        try {
            StringEntity s = new StringEntity(body);
            s.setContentEncoding("UTF-8");
            s.setContentType("multipart/form-data");
            post.setEntity(s);

            HttpResponse response = client.execute(post);
            result = EntityUtils.toString(response.getEntity());
//            result = new String(xmString.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        String test = msgCrypt.decryptMsg(result);
        System.out.println(test);
    }
}
